package com.google.googlemarket;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.googlemarket.utils.Logger;
import com.google.googlemarket.utils.NetManager;

/**
 * Created by Administrator on 2017/9/28.
 */

public class SplashActivity extends Activity {

    private TextView splash_time;
    private LinearLayout splash_skip;
    private Animation mTimeAnimation;
    private boolean jumpTo = false;
    private int mTime = 3;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Logger.i(this, msg.what);
            if (msg.what == 0) {
                getCount();
                if (mTime != 0) {
                    mHandler.sendEmptyMessageDelayed(0, 1000);
                } else {
                    mHandler.sendEmptyMessageDelayed(1, 1000);
                }
            } else if (msg.what == 1 && !jumpTo) {
                jumpTimeSchedule();
            }
        }
    };
    private NetManager netManager;
    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);
        initView();
        initListener();
    }

    private void initView() {
        splash_skip = (LinearLayout) findViewById(R.id.splash_skip);
        splash_time = (TextView) findViewById(R.id.splash_time);

        //加载动画效果
        mTimeAnimation = AnimationUtils.loadAnimation(this, R.anim.splash_anim_page);
        mHandler.sendEmptyMessageDelayed(0, 1000);
    }

    private void initListener() {
        splash_skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpTimeSchedule();
            }
        });

        netManager = new NetManager(this);

    }

    /**
     * 跳过时间进度
     */
    private void jumpTimeSchedule() {
        Logger.i(this, "start jump");
        jumpTo = true;
        if (netManager.isOpenNetwork()) {

            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
        } else {
            ifHasNetwork();
        }
    }

    public void getCount() {
        mTime--;
        splash_time.setText(String.valueOf(mTime));
        mTimeAnimation.reset();
        splash_time.startAnimation(mTimeAnimation);
    }
    /**
     * 判断是否有网络
     * 如果网络不可用，则弹出对话框，对网络进行设置
     */
    private void ifHasNetwork() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("没有可用的网络");
        builder.setMessage("是否对网络进行设置?");

        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent ;

                try {
                    String sdkVersion = android.os.Build.VERSION.SDK;
                    if (Integer.valueOf(sdkVersion) > 10) {
                        intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
                    } else {
                        intent = new Intent();
                        ComponentName comp = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
                        intent.setComponent(comp);
                        intent.setAction("android.intent.action.VIEW");
                    }
                    SplashActivity.this.startActivity(intent);
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SplashActivity.this.finish();
            }
        });

        builder.show();
    }
}
